// 创建用户相关的小仓库
import { defineStore } from 'pinia';
import { reqLogin } from '@/api/user';
import type { loginForm , loginResponseData} from '@/api/user/type.ts';
import type {UserState} from '@/store/modules/types/type.ts';
import { SET_TOKEN , GET_TOKEN} from '@/utils/token.ts';
import {constantRoute} from '@/router/routes.ts';
// 创建用户小仓库
const useUserStore = defineStore('User', {
  // 存储数据的地方
  state: ():UserState => {
    return {
      token: GET_TOKEN(),
      menuRoutes: constantRoute,
    };
  },
  // 异步/逻辑的地方
  actions: {
    // 用户登录的方法
    async userLogin(data: loginForm) {
      //登录请求
      let result:loginResponseData = await reqLogin(data);
      //成功
      if (result.code == 200){
        this.token = result.data.token as string;
        // 本地存储持久化token
        SET_TOKEN(result.data.token as string);
        return 'ok';
      }else {
        //失败
        return Promise.reject(new Error(result.data.message));
      }
    },
  },
  getters: {},
});


export default useUserStore;